import * as THREE from "three";
import { Tshare } from "../utils/Tshare";
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader";
import * as TWEEN from "@tweenjs/tween.js";

let container = document.getElementById("app"),
  T = new Tshare(),
  gltfLoader = new GLTFLoader().setPath("./model/"),
  raycaster = new THREE.Raycaster(),
  selMesh = null,
  pointLight = null;

init();
animate();

function init() {
  T.initEnv(container).initOrbitControls().addStats().loadHdrToScene();
  T.camera.position.set(5, 5, 8);
  T.camera.lookAt(new THREE.Vector3(0, 0, 0));
  T.renderer.shadowMap.enabled = true;
  T.renderer.physicallyCorrentLight = true;
  T.controls.enableDamping = true;
  T.controls.dampingFactor = 0.05;
  T.controls.minDistance = 5;
  T.controls.maxDistance = 20;
  initLight();
  loadGlbModel();
  container.addEventListener("dblclick", raycasterFromScene, false);
}

function animate() {
  requestAnimationFrame(animate);
  T.renderer.render(T.scene, T.camera);
  T.stats.update();
  pointLight.position.x = Math.sin(Date.now() * 0.0005) * 3;
  pointLight.position.y = Math.abs(Math.sin(Date.now() * 0.0005) * 3);
  pointLight.position.z = Math.sin(Date.now() * 0.0015) * 3;
  TWEEN.update();
}
async function loadGlbModel() {
  let { scene } = await gltfLoader.loadAsync("小树1.glb");
  let tree = scene;
  tree.rotateY(-Math.PI / 3);
  tree.position.y -= 0.5;
  tree.traverse((mesh) => {
    if (mesh.isMesh) {
      mesh.castShadow = true;
      mesh.receiveShadow = true;
    }
  });
  T.sceneGraph.add(tree);
}
function initLight() {
  let sphereGeo = new THREE.SphereGeometry(0.06, 20, 20);
  pointLight = getPointsLight(0xeb8dd0, sphereGeo);
  T.scene.add(pointLight, new THREE.AmbientLight(0xffffff, 1));
}
function getPointsLight(color, sphereGeo) {
  let pointLight = new THREE.PointLight(color, 1);
  pointLight.castShadow = true;
  let lightSphere = new THREE.Mesh(
    sphereGeo,
    new THREE.MeshBasicMaterial({ color })
  );
  pointLight.add(lightSphere);
  return pointLight;
}
function cameraAnimate(poiner) {
  controlsTargetChange(T.controls.target, poiner, 2000, (res) => {
    T.camera.position.x = res.x;
    T.camera.position.z = res.z + 5;
    T.camera.position.y = res.y + 1;
    // T.camera.lookAt(res);
  });
}
function raycasterFromScene(e) {
  let mouse = new THREE.Vector2(
    ((e.clientX - container.getBoundingClientRect().left) /
      container.clientWidth) *
      2 -
      1,
    -(
      (e.clientY - container.getBoundingClientRect().top) /
      container.clientHeight
    ) *
      2 +
      1
  );
  raycaster.setFromCamera(mouse, T.camera);
  let intersects = raycaster.intersectObjects(T.sceneGraph.children);
  if (intersects.length > 0) {
    console.log(intersects[0]);
    cameraAnimate(intersects[0].point);
    selMesh = intersects[0].object;
  }
}
// controls的旋转轴的问题
function controlsTargetChange(from, to, time = 1000, updateCallback) {
  new TWEEN.Tween(from)
    .to(to, time)
    .easing(TWEEN.Easing.Quadratic.Out)
    .onStart((res) => {})
    .onUpdate(updateCallback)
    .onStop((res) => {})
    .onComplete((res) => {})
    .start();
}
